package com.fw.mediasession.media.mediastore.database.dao;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.fw.mediasession.media.bean.Music;
import com.fw.mediasession.media.bean.MyPicture;
import com.fw.mediasession.media.bean.MyVideo;
import com.zy.untils.MyLog;

import java.util.ArrayList;

import javax.inject.Inject;

import dagger.hilt.android.qualifiers.ApplicationContext;

/**
 * 访问mediaStore里面的media资源
 */
public class MediaStoreDao {
    private Context context;
    private ArrayList<Music> musics;
    private ArrayList<MyVideo> videos;
    private ArrayList<MyPicture> pictures;

    @Inject
    public MediaStoreDao(@ApplicationContext Context context) {
        this.context = context;
    }

    public ArrayList<Music> getMusicData() {
        musics = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        // 获取歌曲详细信息
        String[] colums = {
                MediaStore.Audio.Media.DATA,// 歌曲文件的路径
                MediaStore.Audio.Media._ID,// 歌曲ID
                MediaStore.Audio.Media.TITLE,// 歌曲标题
                MediaStore.Audio.Media.DISPLAY_NAME,// 歌曲名称
                MediaStore.Audio.Media.MIME_TYPE,// 歌曲的播放格式
                MediaStore.Audio.Media.ARTIST,// 歌曲的歌手名
                MediaStore.Audio.Media.ALBUM,// 歌曲的唱片集
                MediaStore.Audio.Media.IS_RINGTONE,//
                MediaStore.Audio.Media.IS_ALARM,// 歌曲的专辑名
                MediaStore.Audio.Media.IS_MUSIC,//
                MediaStore.Audio.Media.IS_NOTIFICATION,//
                MediaStore.Audio.Media.DURATION // 歌曲的总播放时长
        };
//	      两种方法均可
//			Cursor cursor=
//		    this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
        Cursor cursor = contentResolver.query
                (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, colums, null, null, null);
        while (cursor.moveToNext()) {
            String _id =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
            String filePath =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            String title =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
            String mime_type =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));
//            String duration =
//                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
            long duration =
                    cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
            String DISPLAY_NAME =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
            String artist =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
            String album =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));

            Music music = new Music();
            music.ID = _id;
            music.filePath = filePath;
            music.title = title;
            music.mimeType = mime_type;
            music.duration = duration;
            music.displayName = DISPLAY_NAME;
            music.artist = artist;
            music.album = album;
            musics.add(music);
        }
        MyLog.y("mediaStore的music：" + musics.toString());
        return musics;
    }

    public ArrayList<MyVideo> getVideoData() {
        videos = new ArrayList<>();
        //获取设备上所有的视频信息
        ContentResolver contentResolver = context.getContentResolver();
        String[] videoColumns = new String[]{
                MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.TITLE,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.MIME_TYPE
        };
//      两种方法均可
//		Cursor cursor=
//	    this.managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
        Cursor cursor = contentResolver.query
                (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoColumns, null, null, null);
        while (cursor.moveToNext()) {
            String _id =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
            String filePath =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
            String title =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
            String mime_type =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
            long duration =
                    cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
            MyVideo video=new MyVideo();
            video.ID = _id;
            video.filePath = filePath;
            video.title = title;
            video.mimeType = mime_type;
            video.duration = duration;
            videos.add(video);
        }
        MyLog.y("mediaStore出的视频" + videos.toString());
        return videos;

    }

    public ArrayList<MyPicture> getPictureData() {
        pictures = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        String[] photoColumns = new String[]{
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.TITLE,
                MediaStore.Images.Media.MIME_TYPE,
                MediaStore.Images.Media.SIZE,
                MediaStore.Images.Media.ORIENTATION
        };
//	      两种方法均可
//			Cursor cursor=
//		    this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
        Cursor cursor = contentResolver.query
                (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, photoColumns, null, null, null);
        while (cursor.moveToNext()) {
            String _id =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
            String filePath =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
            String title =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
            String mime_type =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE));
            String size =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE));
            //得到照片旋转角度方法一
            String orientation0 = cursor.getString
                    (cursor.getColumnIndexOrThrow(MediaStore.Images.Media.ORIENTATION));
//            try {
//                ExifInterface exifInterface = new ExifInterface(filePath);
//                String image_length =
//                        exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
//                String image_width =
//                        exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
//                String orientation1 =
//                        exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
//                String dateTime =
//                        exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
//                System.out.println("image_length=" + image_length);
//                System.out.println("image_width=" + image_width);
//                System.out.println("dateTime=" + dateTime);


                MyPicture picture = new MyPicture();
                picture.ID = _id;
                picture.filePath = filePath;
                picture.title = title;
                picture.mimeType = mime_type;
                picture.size = size;
                picture.orientation = orientation0;
                pictures.add(picture);
                //得到照片旋转角度方法二
                //应该结合ExifInterface源码分析.
                //此处有待于进一步分析和验证
//                switch (Integer.valueOf(orientation1)) {
//                    case 1:
//                        System.out.println("第二处旋转角度=" + 0);
//                        break;
//                    case 2:
//                        //matrix.invert(matrix);
//                        break;
//                    case 3:
//                        //matrix.setRotate(180);
//                        System.out.println("第二处旋转角度=" + 180);
//                        break;
//                    case 4:
//                        //matrix.invert(matrix);
//                        //matrix.setRotate(180);
//                        System.out.println("第二处旋转角度=" + 180);
//                        break;
//                    case 5:
//                        //matrix.setRotate(90);
//                        //matrix.invert(matrix);
//                        System.out.println("第二处旋转角度=" + 90);
//                        break;
//                    case 6:
//                        //matrix.setRotate(90);
//                        System.out.println("第二处旋转角度=" + 90);
//                        break;
//                    case 7:
//                        //matrix.invert(matrix);
//                        //matrix.setRotate(90);
//                        System.out.println("第二处旋转角度=" + 90);
//                        break;
//                    case 8:
//                        //matrix.setRotate(270);
//                        System.out.println("第二处旋转角度=" + 270);
//                        break;
//                    default:
//                        break;
//                }

//            } catch (IOException e) {
//                e.printStackTrace();
//            }

        }

        MyLog.y("mediaStore出的图片" + pictures.toString());

        return pictures;

    }
}
